﻿using AntdUI;
using ImportPlcPointPosition.Models;
using ImportPlcPointPosition.Models.Table;
using ImportPlcPointPosition.Models.ViewModels;
using Microsoft.IdentityModel.Tokens;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImportPlcPointPosition.Forms
{
    public partial class VirtualTableForm : BaseForm
    {
        public CksVirtualTable Model = null;
        public VirtualTableForm(CksVirtualTable context = null)
        {
            InitializeComponent();
            if (context != null)
            {
                Model = context;
              
                txtNotes.Text = $"{Model.Notes}";
                txtName.Text = $"{Model.TableName}";
               txtSeq.Text = $"{Model.Sequence}";
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            var item = Model ??  new CksVirtualTable();
            item.Notes = txtNotes.Text;
            item.TableName = txtName.Text;
            item.Sequence = txtSeq.Text.IsNullOrEmpty()?999: txtSeq.Text.ObjToInt();

            if (item.TableName.IsNullOrEmpty())
            {
                AntdUI.Message.error(this, "请填写必填项", autoClose: 2);
                return;
            }
            
            if(item.Id ==0)
                SqlSugarHelper.MesDB.Insertable(item).ExecuteCommand();
            else
                SqlSugarHelper.MesDB.Updateable(item).ExecuteCommand();
            DialogResult = DialogResult.OK;
        }
    }
}
